let city = ''
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})
const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Trading_Type: [{ required: true, message: '请选择打款方式', trigger: 'change' }],
      },
      form: {
      },//表单数据
      formData: {
        Group_Transaction_SerialNumber: "",//主键
        Group_Purchasing_SerialNumber: "",//流水号
        Quantity: "",//总量
        Amount: "",//钱
        Submission_Time: "",//时间
        Shipment_SerialNumber: "",
        Transport_SerialNumber: "",
        Payment_SerialNumber: "",
        Buyer_Contact: "",//买主姓名
        Buyer_Contact_Phone: "",//买主手机号
        Buyer_Enterprise_Info_SerialNumber: $.cookie("enterprise"),//买主公司编号
        Enterprise_Info_SerialNumber: "",//卖家公司编号
      },
      times: '',
      // 整个省市县数据
      city,
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      value: '',
      Trading_Type: [],//打款方式
      Shipment_Type: [],//装运方式
      Transport_Type: [],//运输方式
      Payment_Type: [],//付款方式
      areaList: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      address: '',
      loading: false,
      num: 1,
      dnum: 10,
      fnum: 10,
      hnum: 10,
      span1: '',
      span2: '',
      span3: '',
      span4: '',
    }
  },
  // Quality_Acceptance_Method: '', //质量验收方式
  // Quality_Acceptance_Method: '',  //数量验收方式
  // title: '',  // 标题
  // Delivery_Method: '1',  // 交收方式单选
  // Supplementary_Provisions: '', // 补充条款
  // Guarantee_Method: '',  // 保证金
  mounted() {
    const Group_Purchasing_SerialNumber = sessionStorage.getItem('Group_Purchasing_SerialNumber')
    this.getData(Group_Purchasing_SerialNumber)
  },
  methods: {
    //获取上一页传来的数据
    getData(Group_Purchasing_SerialNumber) {
      let _this = this;
      $.ajax({
        url: DomainName + '/getGroupProcureIDAction',
        type: 'POST',
        data: {
          Group_Purchasing_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.form = res.Data
          console.log(res)
          _this.Trading_Type = res.Paystages
          _this.Shipment_Type = res.Shipment
          _this.Transport_Type = res.Transport
          _this.Payment_Type = res.Payment
          _this.num = res.Data.Starting_Quantity
        }
      })
    },
    // 生成日志
    submitForm() {
      console.log(this.form)
      let this_ = this
      let year = new Date().getFullYear();
      let month = new Date().getMonth() + 1;
      let days = new Date().getDate();
      let hour = new Date().getHours()
      let minutes = new Date().getMinutes()
      let second = new Date().getSeconds()
      if (month < 10) { month = '0' + month }
      if (days < 10) { days = '0' + days }
      if (hour < 10) { hour = '0' + hour }
      if (minutes < 10) { minutes = '0' + minutes }
      if (second < 10) { second = '0' + second }
      var times = year + '-' + month + '-' + days + ' ' + hour + ':' + minutes + ':' + second
      this.formData.Submission_Time = times
      this.formData.Enterprise_Info_SerialNumber = this.form.Enterprise_Info_SerialNumber
      this.formData.Group_Purchasing_SerialNumber = this.form.Group_Purchasing_SerialNumber
      this.formData.Quantity = this.num
      this.formData.Amount = this.form.Amount
      this_.formData.Children = {
        group_transaction_paystages: [
          {
            Group_Transaction_SerialNumber: "",
            Group_Paystages_SerialNumber:"",
            Collection_Date:"",
            Collection_Amount:"",
            Amount:"",
            Trading_Detail_Name: "",
            Trading_Detail_SerialNumber:"",
            Trading_Amount: "",
            Trading_Explain:"",
            Trading_Date: "",
          }
        ]
      }
      if(this_.form.Trading_Name=='按阶段付款'){
        console.log(this_.formData)
        this_.formData.Children.group_transaction_paystages =  this_.Trading_Type
        this_.formData.Children.group_transaction_paystages.forEach(item => {
          item.Group_Paystages_SerialNumber = '',
					item.Group_Transaction_SerialNumber = '',
          item.Amount = this_.form.Amount * item.Trading_Amount * this_.num / 100
          item.Collection_Amount = 0,
          item.Collection_Date = ''
        })
      }else{
        this_.formData.Children.group_transaction_paystages = [
          {
            Amount:this_.form.Amount * this_.num,
            Collection_Amount:0,
            Group_Paystages_SerialNumber:'',
            Group_Transaction_SerialNumber:'',
            Trading_Detail_SerialNumber:this.Trading_Type[0].Trading_Detail_Name,
            Trading_Amount:100,
            Trading_Date:this.Trading_Type[0].Trading_Date,
            Trading_Explain:"全额支付",
            Collection_Date:"",
          }
        ]
      }
      console.log(this.formData)
      var reg = /^1[3-9]\d{9}$/
      if (this.formData.Buyer_Contact == '') {
        this.$message({
          message: '请输入联系人',
          type: 'warning'
        });
        return
      }
      if (this.formData.Buyer_Contact_Phone == '') {
        this.$message({
          message: '请输入联系人手机号',
          type: 'warning'
        });
        return
      }
      if (!reg.test(this.formData.Buyer_Contact_Phone)) {
        this.$message({
          message: '手机号输入有误,请仔细检查',
          type: 'warning'
        });
        return
      }
      if (this.formData.Shipment_Type == '') {
        this.$message({
          message: '请选择装运方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Transport_Type == '') {
        this.$message({
          message: '请选择运输方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Payment_Type == '') {
        this.$message({
          message: '请选择付款方式',
          type: 'warning'
        });
        return
      }
      let _this = this;
      console.log(this.formData)
      if (_this.loading) {
        $.ajax({
          url: DomainName + '/getAddAction',
          type: 'POST',
          data: {
            Data: JSON.stringify({ "group_transaction": [_this.formData] }),
            token: $.cookie("token")
          },
          success: function (res) {
            console.log(res)
            var group_message_buyer = {
              Group_Message_SerialNumber: "",
              Group_Transaction_SerialNumber: res.Data,
              Cause: "提交成功",
              Submission_Time: times,
              Enterprise_Info_SerialNumber: $.cookie("enterprise"),
              Seller_Type: "0"
            }
            var group_message_seller = {
              Group_Message_SerialNumber: "",
              Group_Transaction_SerialNumber: res.Data,
              Cause: "申请提示",
              Submission_Time: times,
              Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
              Seller_Type: "1"
            }
            var group_message = []
            group_message.push(group_message_buyer)
            group_message.push(group_message_seller)
            _this.submitMessage(group_message)
          }
        })
      }
    },
    // 检验库存
    Inventory(Group_Purchasing_SerialNumber) {
      let _this = this
      _this.loading = true
      console.log(Group_Purchasing_SerialNumber, _this.num)
      $.ajax({
        url: DomainName + '/GroupCheckStockAction',
        type: 'POST',
        data: {
          Group_Purchasing_SerialNumber,
          Quantity: _this.num,
          token: $.cookie("token")
        },
        success: function (res) {
          console.log(res)
          if (res.Status == 'success') {
            _this.submitForm()
          } else {
            _this.$notify.error({
              title: '错误',
              message: '库存不足'
            });
          }
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
      })
    },
    // 提交表单
    submitMessage(data) {
      var _this = this
      $.ajax({
        url: DomainName + '/getAddAction',
        type: 'POST',
        data: {
          Data: JSON.stringify({ "group_message": data }),
          token: $.cookie("token")
        },
        success: function (res) {
          _this.loading = false
          console.log(res)
          if (res.Status == 'success') {
            _this.$notify({
              title: '成功',
              message: '企业审核中,请等待',
              type: 'success'
            });
            window.history.go(-1);
          } else {
            _this.$notify.error({
              title: '错误',
              message: '系统错误..'
            });
          }
        }
      })

    },
    changenumber(e) {
      this.formData.Payment_Type = []
      this.span4 = ''
    },
    change1(e) {
      console.log(e)
      if (e == '聚合支付') {
        this.span1 = '第三方支付交易'
      } else {
        this.span1 = '指定账号打款'
      }
    },
    change2(e) {
      console.log(e)
      if (e == '整装') {
        this.span2 = '整装打包'
      } else {
        this.span2 = '散装打包'
      }
    },
    change3(e) {
      console.log(e)
      if (e == '海运') {
        this.span3 = '海运'
      } else if (e == '空运') {
        this.span3 = '空运'
      } else if (e == '陆运') {
        this.span3 = '陆运'
      } else {
        this.span3 = '铁路'
      }
    },
    change4(e) {
      console.log(e)
      let year = new Date().getFullYear();
      let month = new Date().getMonth() + 1;
      let days = new Date().getDate();
      let hour = new Date().getHours()
      let minutes = new Date().getMinutes()
      let second = new Date().getSeconds()
      if (month < 10) { month = '0' + month }
      if (days < 10) { days = '0' + days }
      if (hour < 10) { hour = '0' + hour }
      if (minutes < 10) { minutes = '0' + minutes }
      if (second < 10) { second = '0' + second }
      var times = year + '-' + month + '-' + days + ' ' + hour + ':' + minutes + ':' + second
      if (e == '分期') {
        console.log(this.formData)
        this.formData.Children = {
          group_transaction_paystages: [{
            Group_Transaction_SerialNumber: "",
            Payment_Name: "",
            Payment_Amount: "",
            Payment_Date: ""
          }]
        }
        this.span4 = `鉴于甲方与乙方于${times}签订了产品销售合作协议，根据协议约定乙方应于${this.form.Delivery_Time}前全部支付完货款人民币${this.num * this.form.Amount}元，但至今乙方全部未支付，因此乙方于此确认结欠甲方货款人民币${this.num * this.form.Amount}元，且乙方承诺同意按以下方式分期偿还，现双方就还款达成协议。`
      } else {
        delete this.formData.Children
        console.log(this.formData)
        this.span4 = "全款"
      }
    },
    changed(e) {
      console.log(e)
    },
    // 省份 市 县联动
    selectProvinceFun(event) {
      console.log(event)
      if (event) {
        this.cityList = event.children
      } else {
        this.cityList = []
      }
      this.areaList = []
    },
    selectCityFun(event) {
      console.log(event)
      if (event) {
        this.areaList = event.children
      } else {
        this.areaList = []
      }
    },
    selectAreaFun(event) {
      if (event) {
        console.log(event)
        this.isdisabled = false
      } else {
        this.isdisabled = true
      }
    },
    selectAddress(event) {
      if (event && event != '' && this.selectArea.label != undefined) {
        this.form.Supplier_Address = this.selectProvince.label + '/'
          + this.selectCity.label + '/'
          + this.selectArea.label + '/'
          + this.address
      }
    },
  }
})